requirejs.config({
    baseUrl: 'js',
    paths: {
        zepto : 'zepto.min',   //轻量级仿JQ类库，适合在移动web中使用
        swipe : 'swipe',
        echo  : 'echo.min'
    },
    zepto: {
        exports: '$'
    },
});
require(['zepto'],function(){

    echo.init({
        offset: 0,
        throttle: 0
    });



    // var _tpl = "<div class='cschedule'>\
    //     <ul>\
    //         <li class='active'><div class='radio'></div>推荐的新学员成功报名公开课，可获<br/>取未激活优惠券</li>\
    //         <li><div class='radio'></div>推荐的新学员报名班课,且付费成功</li>\
    //         <li><div class='radio'></div>推荐的新学员上满4课时付费直播课<br/>程</li>\
    //         <li><div class='radio'></div>优惠券激活成功</li>\
    //     </ul>\
    // </div><div class='close-btn'><i class='icon iconfont icon-guanbi1'></i></div>";
    // layer.open({
    //     shadeClose:false,
    //     className: 'cschedule_popuo',
    //     shade:true,
    //     content: _tpl,
    //     success: function(elem){
            
    //     }
    // });

    //绑定手机号
    function bangding(o){
        $('.bound_mobile_messge').remove();
        var bound_mobile_tpl = "<div class='bound_mobile_messge'>\
            <div class='bound_message_mask'></div>\
            <div class='bound_message_section bounceIn'>\
                <div class='bound_message_content'>\
                    <div class='bound_message_child'>\
                        <div class='bound_mobile'>\
                            <div class='txt'>\
                                <h2>手机号绑定</h2>\
                                <p>"+o.hint+"</p>\
                            </div>\
                            <div class='bound-from'>\
                                <div class='error'></div>\
                                <div class='input_from'><div class='input_group'><input type='tel' class='weui-input' name='mobile' placeholder='输入手机号码'></div></div>\
                                <div class='input_from'><div class='input_group'><input type='tel' class='weui-input' name='passcode' placeholder='输入验证码'></div><button class='weui-btn weui-btn_codevv weui-btn_disabled' disabled='disabled'>获取验证码</button></div>\
                                <button class='weui-btn weui-btn_login weui-btn_disabled' disabled='disabled'>确定</button>\
                            </div>\
                        </div>\
                        <div class='close_btn'><i class='icon iconfont icon-guanbi1'></i></div>\
                    </div>\
                </div>\
            </div>\
        </div>";
        $('body').append(bound_mobile_tpl);
        var _elem = $('.bound_mobile_messge')
        function isPoneAvailable(str) {  
            var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;  
            if (!myreg.test(str)) {  
                return false;  
            } else {  
                return true;  
            }  
        }  
        var _elem = $('.bound_mobile_messge'),
        _bound_mobile_btn = $(".weui-btn_login",_elem),
        _weui_codevv = $(".weui-btn_codevv",_elem),
        _error = $(".error",_elem),
        _mobile = $("input[name='mobile']",_elem),
        _passcode = $("input[name='passcode']",_elem);
        $(".icon-guanbi1",_elem).on('click',function(){
            _elem.fadeOut(function(){
                _elem.remove();
            });
            // $(".bound_message_section",_elem).removeClass('bounceIn').fadeOut(function() {
            //     _elem.remove();
            // });
        })
        function setBoundMobileBtn(){
            if(isPoneAvailable(_mobile.val().trim()) && _passcode.val().trim()){
                _bound_mobile_btn.removeAttr('disabled style').removeClass('weui-btn_disabled');
            }else{
                _bound_mobile_btn.attr('disabled',true).addClass('weui-btn_disabled');
            }
        }
        _mobile.on('input', function(e) {
            if(isPoneAvailable($(this).val().trim())){
                _weui_codevv.removeAttr('disabled').removeClass('weui-btn_disabled');
            }else{
                _weui_codevv.attr('disabled',true).addClass('weui-btn_disabled');
            }
            setBoundMobileBtn()
        })
        _passcode.on('input', function(e) {
            setBoundMobileBtn()
        })
        //绑定手机号获取验证码
        _weui_codevv.on('click',function(e){
            var _that = $(this),count,_timer;
            var _params = {
                type:12,
                mobile:_mobile.val().trim(),
            }
            function countDown(o){
                _mobile.attr('disabled',true);
                _that.attr('disabled',true).addClass('weui-btn_disabled').text('60秒后重发'),count = 60;
                _timer && clearInterval(_timer);
                _timer = setInterval(function(){
                    count--;
                    if (count > 0){
                        o.text(count + "秒后重发");
                    }else {
                        _timer && clearInterval(_timer);
                        _mobile.removeAttr('disabled style');
                        o.removeAttr('disabled style').removeClass("weui-btn_disabled").text("获取验证码");
                    }
                }, 1000);
            }
            countDown(_that);
            $.post('http://api.dev.chaojizhiyuan.com/user/v2/send_passcode',_params, function(data){
                if(data.status == 0){
                    layer.open({content: '发送成功,请查收短信',skin: 'msg',time: 2});
                }else{
                    _timer && clearInterval(_timer);
                    _mobile.removeAttr('disabled style');
                    _that.removeAttr('disabled style').removeClass("weui-btn_disabled").text("获取验证码");
                    if(data.status == 130){
                        var captcha_guid = data.captcha_guid;
                        var captcha_url = data.captcha_url;
                        var _TplStr = "<h2>请在下方输入图形验证码</h2>\
                            <p class='port_error'></p>\
                            <div class='close_btn'><i class='icon iconfont icon-guanbianniu'></i></div>\
                            <div class='picture'>\
                                <div class='input-g'><input type='text' name='code' class='check_code' autofocus='true'/></div>\
                                <div class='pic-g'><img class='pic_code'/><span class='checkcode'>看不清换一张</span></div>\
                            </div>\
                        <button class='weui-btn weui-btn_mini verify_btn weui-btn_disabled' disabled='disabled'>确定</button>";
                        layer.open({
                            className: 'popuo-picture',
                            shadeClose: false,
                            content: _TplStr,
                            success: function(y_elem){
                                //获取图像验证码
                                function getCheck(){
                                    $('.pic_code',y_elem).attr('src',""); 
                                    captcha_guid = '';
                                    $.getJSON('http://api.dev.chaojizhiyuan.com/user/v2/captcha_pair',{mobile:_params.mobile},function(o){
                                        if(o.status === 0){
                                            $('.pic_code',y_elem).attr('src',o.captcha_url); 
                                            captcha_guid = o.captcha_guid;
                                        }
                                    })
                                }
                                //获取图像验证码
                                $('.pic_code',y_elem).attr('src',captcha_url); 
                                //刷新图像验证码
                                $('.checkcode',y_elem).on('click',function(e){
                                    getCheck()
                                })
                                $("input[name='code']",y_elem).on('input', function(e) {
                                    if($(this).val().trim()){
                                        $('.verify_btn',y_elem).removeAttr('disabled').removeClass('weui-btn_disabled');
                                    }else{
                                        $('.verify_btn',y_elem).attr('disabled',true).addClass('weui-btn_disabled');
                                    }
                                })
                                //关闭图片验证码
                                $('.close_btn',y_elem).on('click',function(e){
                                    layer.close(y_elem.getAttribute('index'));
                                })
                                //验证输入的验证码和给的验证码
                                $('.verify_btn',y_elem).on('click',function(e){
                                    $(this).attr('disabled',true).addClass('weui-btn_disabled').text('验证中...');
                                    var yparams = _params
                                    yparams.captcha_guid = captcha_guid
                                    yparams.captcha = $("input[name='code']",y_elem).val().trim();
                                    $.getJSON('http://api.dev.chaojizhiyuan.com/user/v2/validate_captcha',yparams,function(o){
                                        if(o.status === 0){
                                            $('.port_error',y_elem).hide().text('');
                                            layer.close(y_elem.getAttribute('index'));
                                            countDown(_that);
                                            $.post('http://api.dev.chaojizhiyuan.com/user/v2/send_passcode',yparams, function(data){
                                                if(data.status === 0){
                                                    layer.open({content: '发送成功,请查收短信',skin: 'msg',time: 2});
                                                }else{
                                                    _timer && clearInterval(_timer);
                                                    _mobile.removeAttr('disabled style');
                                                    _that.removeAttr('disabled style').removeClass("weui-btn_disabled").text("获取验证码");
                                                }
                                            })
                                        }else{
                                            $('.port_error',y_elem).show().text(o.error_info);
                                        }
                                        $('.verify_btn',y_elem).removeAttr('disabled').removeClass('weui-btn_disabled').text("确定");
                                    })
                                })
                            }
                        });
                    }else{
                        common.commonPopop(data.error_info)
                    }
                }
            })
        })
        //绑定手机号
        _bound_mobile_btn.on('click',function(e){
            var _that = $(this)
            _that.attr('disabled',true).addClass('weui-btn_disabled').text('绑定中...');
            var params = {
                mobile:_mobile.val().trim(),
                passcode:_passcode.val().trim()
            }
            //common.spinLoading('绑定中...')
            $.post('http://api.dev.chaojizhiyuan.com/user/v2/weixin/bind_mobile_web',params, function(data){
                layer.closeAll();
                if(data.status == 0){
                    common.saveUserdata(data)
                    common.setHistoryTelnum(params.mobile)
                    _elem.remove();
                }else{
                    common.commonPopop(data.error_info)
                    _that.removeAttr('disabled').removeClass('weui-btn_disabled').text('确定');
                }
            })
        }) 
    }
    bangding({hint:'报名前需要先绑定手机号哦~'})
    //绑定手机号



    var seminary = $("#seminary"),sidemenu_filter_dialog = $("#sidemenu-filter-dialog"),isopen = false;
    seminary.on('click',function(){
        if(isopen && $(this).hasClass('open')){
            isopen = false;
            $(this).removeClass('open')
            sidemenu_filter_dialog.removeClass('overlapping')
        }else{
            isopen = true;
            $(this).addClass('open')
            sidemenu_filter_dialog.addClass('overlapping')
        }
    })
    //列表上的按钮添加事件
    //course-type:1公开课、2收费班课
    $(".product_lists").on('click','.weui-btn',function(){
        console.log($(this).data('course-type'))
    })


    var intro_cover = $("#intro_cover");
    intro_cover.height($(window).width()*9/16)

    $(".menu-nav-button").on('click',function(){
        // $(".nav-menu-cover").css('display','block')
        // requestAnimationFrame(function(){
        //     $(".nav-menu-cover").addClass('show')
        // });
        $(".nav-menu-cover").addClass('show')
    })
    //console.log($.tap())
    $(".nav-menu-cover .cover-bg").on('touchstart',function(){
        $(".nav-menu-cover").removeClass('show')
    })
});